import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: "/login",
    component: () => import('../views/login/Login')
  },
  {
    path: "/register",
    component: ()=>import('../views/register/Register')
  },
  {
    path: "/forget",
    component: ()=>import('../views/forget/Forget')
  },
  {
    path: "/test",
    component: () => import('../views/login/Test')
  },
  {
    path: "/home",
    component: ()=>import('../views/home/Home'),
    redirect: '/home/dashboard',
    children: [{
      path: "dashboard",
      component: ()=>import('../views/home/DashBoard'),
    },{
      path: "computer",
      component: ()=>import('../views/home/Computer'),
    }]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next();
  }
  if (window.localStorage.getItem("token") === null) {
    console.log("token is null");
    alert("token过期或为空，请重新登录");
    return next('/login');
  }
  next();
})

export default router
